package work3.work1;

import Uti.ScannerUti;
import work3.work1.Goods;

import java.util.Arrays;

public class Customer {
    private String name;
    private int age;
    private Goods[] goods=new Goods[5];//采购商品
    private double total; //总采购金额
    private int nums;
    Customer() {
    }
    void inputGoodsInfo() {
        System.out.println("请输入商品 名称，单价，数量；以空格分隔，按n结束");
        while (true) {
            Goods good1 = new Goods();

            String s = ScannerUti.getSting();
            if (s.equals("n")) break;

            String[] split = s.split(" ");
            good1.setName(split[0]);
            try {
                good1.setPrice(Double.parseDouble(split[1]));
                good1.setNum(Integer.parseInt(split[2]));
            } catch (NumberFormatException e) {
                System.out.println("输入有误请重新输入");
                System.out.println("请输入商品 名称，单价，数量；以空格分隔，按n结束");
                /*
                apple 2.2 10
car 1000.0 2
bike 500 3
                 */
                continue;
            }
            good1.setTotalPrice();
            goods[nums++] = good1;
            total+=goods[nums-1].getTotalPrice();
            if (nums %5==0) {
                goods = Arrays.copyOf(goods, nums + 5);
            }


        }
        /*for(int i=0;i<nums;i++){
            System.out.println(goods[i].toString());
        }*/
    }

    public Customer(String name, int age, Goods[] goods, double total) {
        this.name = name;
        this.age = age;
        this.goods = goods;
        this.total = total;
    }  //构造方法




    /**
     * 获取
     *
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     *
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     *
     * @return age
     */
    public int getAge() {
        return age;
    }

    /**
     * 设置
     *
     * @param age
     */
    public void setAge(int age) {
        this.age = age;
    }

    /**
     * 获取
     *
     * @return goods
     */
    public Goods[] getGoods() {
        return goods;
    }

    /**
     * 设置
     *
     * @param goods
     */
    public void setGoods(Goods[] goods) {
        this.goods = goods;
    }

    /**
     * 获取
     *
     * @return total
     */
    public double getTotal() {
        return total;
    }

    /**
     * 设置
     *
     * @param total
     */
    public void setTotal(double total) {
        this.total = total;
    }

    public void print() {
        System.out.println(String.format("姓名：%-10s;年龄：%-10s", name,age));
        System.out.println("-----------------------");
        for(int i=0;i<nums;i++){
            System.out.println(goods[i].toString());
        }
        if(nums>0){
            System.out.println("总计:"+total);
            System.out.println();
        }

    }
}
